package logic

import (
	"context"
	"errors"
	"goods_srv/goods"
	"goods_srv/internal/svc"
	"goods_srv/models/mysqlinit"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateHouseLogic {
	return &UpdateHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateHouseLogic) UpdateHouse(in *goods.UpdateHouseRequest) (*goods.UpdateHouseResponse, error) {
	// todo: add your logic here and delete this line
	var houses mysqlinit.House
	mysqlinit.DB.Debug().Where("user_id = ?", in.UserId).Find(&houses)
	if houses.UserId != int(in.UserId) {
		logx.Error("无法修改他人房源")
		return nil, errors.New("无法修改他人房源")
	}
	m := mysqlinit.House{

		HouseTitle: in.HouseTitle,
		HouseRent:  float64(in.HouseRent),
		Address:    in.Address,
		Community:  in.Community,
		FloorCode:  in.FloorCode,
		FloorNum:   int(in.FloorNum),
		HouseArea:  int(in.HouseArea),
		//Status:     in.Status,
	}
	mysqlinit.DB.Debug().Where("id = ?", in.Id).Updates(&m)

	return &goods.UpdateHouseResponse{
		//Id: int64(m.ID),
	}, nil
}
